"""Command line entry point for the FlowMap toolchain."""

from __future__ import annotations

import argparse
import sys

from .commands import analyze, instrument, report


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="flowmap", description="Unified flow map CLI")
    subparsers = parser.add_subparsers(dest="command")

    analyze.register(subparsers)
    instrument.register(subparsers)
    report.register(subparsers)

    return parser


def main(argv: list[str] | None = None) -> int:
    parser = build_parser()
    args = parser.parse_args(argv)

    if not args.command:
        parser.print_help()
        return 0

    handler = getattr(args, "handler", None)
    if handler is None:
        parser.error("No handler associated with the command")
        return 1

    return handler(args)


if __name__ == "__main__":
    sys.exit(main())
